package com.qd.flow;

import com.cdqidi.dto.ApiResult;
import com.cdqidi.dto.ApiResultWrapper;
import com.cdqidi.dto.DelDTO;
import com.cdqidi.exception.ApiException;
import com.cdqidi.valid.group.DefaultGroup;
import com.cdqidi.valid.group.UpdateGroup;
import com.qd.common.flow.domain.config.FlowProcessNodeDTO;
import com.qd.common.sys.annotation.OperatorInfo;
import com.qd.common.sys.enums.OperatorType;
import com.qd.common.sys.util.SecurityUtils;
import com.qd.flow.service.config.FlowProcessConfigService;
import com.qd.flow.service.config.FlowProcessNodeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 流程/WEB_流程节点配置
 *
 * @author sjk
 */
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/sys/flowProcessNode/")
@Slf4j
public class FlowProcessNodeController {
    private final FlowProcessNodeService flowProcessNodeService;
    private final FlowProcessConfigService flowProcessConfigService;

    /**
     * 添加
     *
     * @param dto 页面对象
     * @return 页面对象
     */
    @PreAuthorize("@ps.hasPermission('system:flowProcessNode:save')")
    @OperatorInfo(title = "添加", operator = OperatorType.INSERT)
    @PostMapping(value = "/save")
    public FlowProcessNodeDTO save(@Validated({DefaultGroup.class}) @RequestBody FlowProcessNodeDTO dto) {
        if (Boolean.FALSE.equals(flowProcessConfigService.isExistsById(dto.getProcessId()))) {
            throw new ApiException("流程ID值错误");
        }
        dto.setOperator(SecurityUtils.getLoginUser().getUserId());
        dto.setOrgId(SecurityUtils.getLoginUser().getCurrentOrgId());
        dto.setAddTime(LocalDateTime.now());
        return flowProcessNodeService.save(dto);
    }

    /**
     * 修改
     *
     * @param dto 页面对象
     * @return 页面对象
     */
    @PreAuthorize("@ps.hasPermission('system:flowProcessNode:edit')")
    @OperatorInfo(title = "修改", operator = OperatorType.UPDATE)
    @PutMapping(value = "/update")
    public FlowProcessNodeDTO update(@Validated({UpdateGroup.class, DefaultGroup.class}) @RequestBody FlowProcessNodeDTO dto) {
        if (Boolean.FALSE.equals(flowProcessConfigService.isExistsById(dto.getProcessId()))) {
            throw new ApiException("流程ID值错误");
        }
        dto.setOperator(SecurityUtils.getLoginUser().getUserId());
        dto.setOrgId(SecurityUtils.getLoginUser().getCurrentOrgId());
        dto.setUpdateTime(LocalDateTime.now());
        return flowProcessNodeService.update(dto);
    }

    /**
     * 删除
     *
     * @param delDTO delDTO中的ids,主键
     * @return 成功 or 失败
     */
    @PreAuthorize("@ps.hasPermission('system:flowProcessNode:delete')")
    @OperatorInfo(title = "删除", operator = OperatorType.DELETE)
    @DeleteMapping(value = "/delete")
    public ApiResultWrapper<Object> delete(@Validated @RequestBody DelDTO<String> delDTO) {
        List<String> ids = delDTO.getIds();
        if (ids.size() == 1) {
            flowProcessNodeService.removeById(ids.get(0));
        } else {
            flowProcessNodeService.removeBatchIds(ids);
        }
        return ApiResult.success("删除成功");
    }

    /**
     * 全部
     *
     * @param dto 查询参数以及页数和每页显示条数
     * @return 列表信息
     */
    @GetMapping(value = "/list")
    public List<FlowProcessNodeDTO> list(FlowProcessNodeDTO dto) {
        if (null == dto) {
            throw new ApiException("流程ID值不能为空");
        }
        if(!StringUtils.hasLength(dto.getProcessId())){
            throw new ApiException("流程ID值不能为空");
        }
        if (Boolean.FALSE.equals(flowProcessConfigService.isExistsById(dto.getProcessId()))) {
            throw new ApiException("流程ID值错误");
        }
        return flowProcessNodeService.getListDto(dto);
    }
}